/**
*交通状况 js
*/ 

var	trafficinfoDS;
var trafficinfoFormPanel;
function doTrafficDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JTrafficInfoWeb.deleteTrafficInfoBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					trafficinfoDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//trafficinfoFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var trafficinfoType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'road', type: "string"},
							   {name: 'roadway', type: "string"},
							   {name: 'distanceC', type: "string"}, 
							   {name: 'arrivaltimeC', type: "string"}, 
							   {name: 'distanceP', type: "string"},  
							   {name: 'arrivaltimeP', type: "string"}, 
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'}
						 ]);
	var trafficinfoCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},
				{header: "公路状况",  sortable: true, dataIndex: 'roadway',align:'left'},
				{header: "路面状况",  sortable: true, width: 200,dataIndex: 'road',align:'left'},	
				{header: "路程（公里）",  sortable: true, dataIndex: 'distanceC',align:'left'},	
				{header: "时间",  sortable: true, dataIndex: 'arrivaltimeC',align:'left'},	   
				{header: "路程（米）",  sortable: true, dataIndex: 'distanceP',align:'left'}	    				
				//{header: "更新日期",  width: 150,sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d h:M:s'),dataIndex: 'refreshtime',align:'left'},	 
				//{header: "删除", dataIndex: 'id',renderer: showDelete}					
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doTrafficDelete(\"" + id  +   "\")'>";  
	}
		
	trafficinfoDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JTrafficInfoWeb.getTrafficInfoList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, trafficinfoType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var trafficinfoGrid = new Ext.grid.GridPanel({			 	
			ds: trafficinfoDS,
			cm: trafficinfoCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            trafficinfoFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: trafficinfoDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});
		
	trafficinfoDS.on('load', function() {
		trafficinfoGrid.getSelectionModel().selectFirstRow(); 
	});		

	var roadStore = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var roadCombo = new Ext.form.ComboBox({	   
			fieldLabel: '路面状况',
			name: 'road',	
			width:120,
			valueField :"value",
			displayField: "text",
			mode: 'remote',
			selectOnFocus:true,                    
			forceSelection: true,  
			hiddenName:'road',
			triggerAction: 'all', 
			editable:false, 
			disabled:true,
			store: roadStore
		});	
	
	roadStore.on('beforeload', function() {
			  roadStore.baseParams = {
					DIRECTORY_TYPE:'10'   
			  };
		});	
	roadStore.load({params:{DIRECTORY_TYPE:'10'}});
	
	
	var roadwayStore = new Ext.data.Store({  
        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
        reader:new Ext.data.DWRArrayReader(
				{},
				new Ext.data.Record.create([  
					{name: 'text', mapping:1},       
					{name: 'value',mapping:0}                   
				  ])
				)  
    }); 

	var roadwayCombo = new Ext.form.ComboBox({	   
		fieldLabel: '公路状况',
		name: 'roadway',	
		width:120,
		valueField :"value",
		displayField: "text",
		mode: 'remote',
		selectOnFocus:true,                    
		forceSelection: true,  
		hiddenName:'roadway',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: roadwayStore
	});	
	
	roadwayStore.on('beforeload', function() {
		roadwayStore.baseParams = {
				DIRECTORY_TYPE:'35'   
		  };
	});	
	roadwayStore.load({params:{DIRECTORY_TYPE:'35'}});
		
	
	trafficinfoFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 190,
		minSize: 200,
		maxSize: 200,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.20,
				layout: 'form',  
				labelWidth:60,	 
				labelAlign:'right',	
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:100,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:120,
						disabled:true,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:120,
						disabled:true,
						readOnly: true,
						name: 'btsname'
					},roadwayCombo
					 ,roadCombo
					
					]
			 },
			 {
				columnWidth:.40,
				layout: 'form',  
				labelWidth:270,	
				labelAlign:'right',		
				labelAlign :'right', 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '路程（公里）--正常情况下车辆达到基站的路程',
						width:100,
						value:-1,
						disabled:true,
						name: 'distanceC'
					}, {
						xtype:'textfield',
						fieldLabel: '时间（分钟）',
						width:100,
						value:-1,
						disabled:true,
						name: 'arrivaltimeC'
					},{
						xtype:'textarea',
						fieldLabel: '备注',
						width:100,
						disabled:true,
						name: 'memo'
					}
					]
			 },
			 {
				columnWidth:.40,
				layout: 'form',  
				labelWidth:270,	 
				labelAlign:'right',	
				labelAlign :'right', 
				items: [ 
					{
						xtype:'textfield',
						fieldLabel: '路程（米）--人员达到基站点的路程',
						width:100,
						value:-1,
						disabled:true,
						name: 'distanceP'
					},{
						xtype:'textfield',
						fieldLabel: '时间',
						width:100,
						value:-1,
						disabled:true,
						name: 'arrivaltimeP'
					}]
			 }
			] 	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'trafficinfoTab',
		items    : [
			trafficinfoGrid,
			trafficinfoFormPanel
		]
	});
	
	
	function savetrafficinfo()
	{
		if (!trafficinfoFormPanel.getForm().isValid())
			return;	
			
		var form = trafficinfoFormPanel.getForm().getValues(false);	
		TrafficInfoBO = {};
		TrafficInfoBO.id = form.id;
		TrafficInfoBO.btscode = form.btscode;
		TrafficInfoBO.btsname= form.btsname;
		TrafficInfoBO.roadway = form.roadway;
		TrafficInfoBO.road = form.road;
		TrafficInfoBO.distanceC = form.distanceC;
		TrafficInfoBO.arrivaltimeC = form.arrivaltimeC;
		TrafficInfoBO.distanceP = form.distanceP;
		TrafficInfoBO.arrivaltimeP = form.arrivaltimeP;
		TrafficInfoBO.memo = form.memo;
		 	   			
		JTrafficInfoWeb.saveTrafficInfoBO(TrafficInfoBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				trafficinfoDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	
				trafficinfoFormPanel.getForm().reset(); 
				var dataTowerMounte = {
										'btscode':"<%=strbtscode%>",
										'btsname':"<%=strbtsname%>"
									};	 		
				var recordTowerMounte = new Ext.data.Record(dataTowerMounte); 
				trafficinfoFormPanel.getForm().loadRecord(recordTowerMounte);	 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
	loadDate(trafficinfoDS,trafficinfoFormPanel);
	
	
})